// RMSServer.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "RMSServer.h"
#include "RMSServerDlg.h"
#include "RegistrationDlg.h"
#include "..\Common\XMessageBox.h"

bool bRibig_Login=false;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _NET_VC_

#ifndef _VC_J7_
	#define APP_NAME	"NetVC Encoder"
#else
	#define APP_NAME	"VC-J7 Encoder"
#endif


#elif defined VC70
	#define APP_NAME	"VC-70 Encoder"
#else
	#define APP_NAME	"VC-10 Encoder"
#endif

bool gUserModeEnabled=false;

#ifdef _IPR_
  bool gEnableIpr;
#endif //def _IPR_

/////////////////////////////////////////////////////////////////////////////
// CRMSServerApp

BEGIN_MESSAGE_MAP(CRMSServerApp, CWinApp)
	//{{AFX_MSG_MAP(CRMSServerApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRMSServerApp construction

CRMSServerApp::CRMSServerApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CRMSServerApp object

CRMSServerApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CRMSServerApp initialization

#include <algorithm>

void* MainDlgPtr=0;

#include "../Common/sfec.h"

extern int EnableFEC;
extern bool DisableFEC;
extern int FECparam1;
extern int FECparam2;
unsigned int bindIP=0;


BOOL CRMSServerApp::InitInstance()
{
//#define EXPIRED 
#ifdef _OEM_
	#ifndef _TEC_ // no expiration for TEC !
		#define EXPIRED
	#endif
#endif

    string s(m_lpCmdLine);
    CSimpleCmdLine cl(s);

    gUserModeEnabled = false;
#ifdef _GUI2008_
    gUserModeEnabled = true;
    gUserModeEnabled = !cl.GetParam("admin");
#endif

    DisableFEC = cl.GetParam("nofec");
    if(!DisableFEC)
    {
      EnableFEC=0;
      if (cl.GetParam("fec")) EnableFEC=1; else
      if (cl.GetParam("2xfec")) EnableFEC=2;
      FECparam1 = cl.GetIntParam("packsize", 174); // max packet size (174 = 5*33 +9)
      FECparam2 = cl.GetIntParam("seq", 3); // max consecutive packets to recover
    }

    if (cl.GetParam("ip"))
    {
      int i;
      string ip = cl.GetStrParam("ip");
      for(i=0; i<ip.length(); i++) ip[i] = toupper(ip[i]);

      if (ip=="ANY")
        bindIP=0;
      else
        bindIP = inet_addr(ip.c_str());
    } else bindIP = 1;

#ifdef _IPR_
	gEnableIpr = false;
	gEnableIpr = cl.GetParam("ipr");
#endif //def _IPR_

#ifdef EXPIRED

#ifdef _OEM_
	int MAX_DAYS=30;
	SetRegistryKey("Streambox_OEM");
#else
	int MAX_DAYS=7;
//	int MAX_DAYS=30;
	SetRegistryKey("Streambox");
#endif

    int RunTime;
    int DaysLeft=MAX_DAYS;

    RunTime = GetProfileInt("","run0",-1);
    DaysLeft = GetProfileInt("","run1",-1);

    bool bExpired=false;
    int day;
    
    if (RunTime!=-1) {
        if (abs(time(0)-RunTime) > 24*3600) {
            DaysLeft -= abs(time(0)-RunTime) / (24*3600);
            WriteProfileInt("","run0",time(0));
            WriteProfileInt("","run1",DaysLeft);
        }
        
        if (DaysLeft<=0)
//            bExpired=true;
            bExpired=false;
    }
    else {
        WriteProfileInt("","run0",time(0));
        WriteProfileInt("","run1",MAX_DAYS);
        DaysLeft=MAX_DAYS;
    }


    if (bExpired) {
         MessageBox(NULL, _T("The application has expired. Please contact \nsupport@streambox.com for more information."),
					_T(APP_NAME), MB_OK | MB_ICONWARNING);
         return FALSE;
    }
    else {
        char szTrialMessage[100];
        sprintf(szTrialMessage,"You are on day %d of your %d-day trial period\n\nOnly %d day(s) left to buy a full version",MAX_DAYS+1-DaysLeft,MAX_DAYS,DaysLeft);
               
        
        if (XMessageBox(NULL,szTrialMessage,"Trial Reminder",MB_STARTBUY | MB_NORESOURCE,0)==IDBUY) {
            ShellExecute(0,"open","http://www.streambox.com","","",SW_SHOW);
            return FALSE;            
        }    
    }
#else
	bool bRegistration = false;


 #ifndef _OEM_
  #ifdef _NET_VC_
   #ifndef _HCP_
    #ifndef _QFI_NCP_
	CTime Time = CTime::GetCurrentTime();
 	CTime haltTime(2010, 12, 31, 23 , 59, 59);
  	if(Time >= haltTime){
  		MessageBox(NULL, _T("The application has expired. Please contact \n"
							"support@streambox.com for more information."),
						 _T(APP_NAME), MB_OK | MB_ICONWARNING);
  		return FALSE;
  	}
    #endif //ndef _QFI_NCP_
   #endif //ndef _HCP_
  #endif //def _NET_VC_
 #endif //ndef _OEM_

	SetRegistryKey("Streambox");

	// Registration key
	char szName[256];
	char szKey[KEY_LENGTH+1];

   if(bRegistration)
   {
	if(REGISTRY_LoadString(KEY_REGISTRATION_NAME, szName, 256))
	{
		if(REGISTRY_LoadString(KEY_REGISTRATION_KEY, szKey, KEY_LENGTH+1))
		{
			if(CheckKey(szName, szKey))
			{
				bRegistration = false;
			}
		}
	}
   }

#ifdef _J707_PENDING_ //#J7070020
   bRegistration = false;
#endif
	if(bRegistration)
	{
		CRegistrationDlg dlg;
		int count = 0;
		while(count < 5)
		{
			if(dlg.DoModal() == IDOK)
			{
				if(CheckKey(static_cast<LPCTSTR>(dlg.m_strName), static_cast<LPCTSTR>(dlg.m_strKey)))
				{
					bRegistration = false;
					REGISTRY_SaveString(KEY_REGISTRATION_NAME, static_cast<LPCTSTR>(dlg.m_strName));
					REGISTRY_SaveString(KEY_REGISTRATION_KEY, static_cast<LPCTSTR>(dlg.m_strKey));
					break;
				}
				else
				{
					MessageBox(NULL, _T("Registration key is not valid."), _T(APP_NAME), MB_OK | MB_ICONWARNING);
				}
			}
			else
			{
				return FALSE;
			}
			count++;
		}
	}

	if(bRegistration) return FALSE;
#endif

#ifdef _OEM_
#ifndef _TEC_
	CTime Time = CTime::GetCurrentTime();
 	CTime haltTime(2005, 4, 20, 0 , 0, 0);
  	if(Time >= haltTime){
  		MessageBox(NULL, _T("The application has expired. Please contact \nsupport@streambox.com for more information."),
				   _T(APP_NAME), MB_OK | MB_ICONWARNING);
  		return FALSE;
  	}
#endif
#endif


	WORD wVersionRequested;
	WSADATA wsaData;
 
	wVersionRequested = MAKEWORD( 2, 0 );
 
	WSAStartup( wVersionRequested, &wsaData );

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

           CRMSServerDlg* pp1,*pp2;
           void* p1,*p2;

	CRMSServerDlg dlg;
	MainDlgPtr = m_pMainWnd = &dlg;

	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

#ifdef _HCP_
	// Logout only when logged in
	if( bRibig_Login == true )
	{
		/*
		char szErrorMessage[100];
		int result = rLogOut_MatrixNet(34987, 1, 1);
		if( result < 0 )
		{
			sprintf(szErrorMessage, "Ribig: Logout error %d\n", result );
			MessageBox(NULL, _T(szErrorMessage),
						 _T(APP_NAME), MB_OK | MB_ICONWARNING);
		}

		// End Session
		rRelease_MatrixAPI();
		*/
	}
#endif
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
